/*Aitor Gonzalez Aguirre 	(NIA: 47757) aitor.gonzalez02@campus.upf.edu
Agustin Marziani 	(NIA: 69236) agustin.marziani01@campus.upf.edu*/

//Vectores
varying vec3 normal; 
varying vec3 light_vector[2]; 
varying vec3 reflected_vector[2];
varying vec3 eye_vector;

//Colores
uniform vec3 light_color[2];
uniform vec3 ambient_factor;
uniform vec3 diffuse_factor;
uniform vec3 specular_factor;

//Intensidades
uniform float ambient_intensity;
uniform float diffuse_intensity;
uniform float specular_intensity;

//Glossiness
uniform float glossiness;

//Ojo de la camara
uniform vec3 camera_eye;

void main()
{
	vec3 pixel_color;
	float LdotN[2], RdotV[2];
	
	normal = normalize (normal);
	for (int i=0; i<2; i++) {
		reflected_vector[i] = normalize (reflected_vector[i]);
		
		//calcule the NdotL
		LdotN[i] = dot (light_vector[i], normal);
		//avoid negative values (it doesnt makes sense to have negative light)
		if (LdotN[i] < 0.0) LdotN[i] = 0.0;
		
		//calcule the RdotV
		RdotV[i] = dot (reflected_vector[i], eye_vector);
		//avoid negative values (it doesnt makes sense to have negative light)
		if (RdotV[i] < 0.0) RdotV[i] = 0.0;
	}
	
	//////////////////////////////FORMULA//////////////////////////////////////////
	vec3 ambient_color, diffuse_color[2], specular_color[2], pixel_intensity[2];
	
	ambient_color = ambient_factor * ambient_intensity;
	
	pixel_color = ambient_color;
	for (int i=0; i<2; i++) {
		diffuse_color[i] = diffuse_factor * LdotN[i] * diffuse_intensity;
		specular_color[i] = specular_factor * pow(RdotV[i], glossiness) * specular_intensity;
		pixel_intensity[i] =  diffuse_color[i] + specular_color[i];
		//store the resulting color in the varying var to use it from the pixel shader
		pixel_color += (pixel_intensity[i]  * light_color[i]);
	}
	
	//store the final color in the FragColor
	gl_FragColor = // vec4(pixel_color,1.0); //the last value is the alpha
}
